package com.flyee.utils
{
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.geom.Matrix;
	import flash.geom.Point;
	/**
	 * 绘制环形图形的类
	 * @author <>
	 * 
	 */	
	public class CamberGraph
	{
		/**
		 * 圆心点坐标 
		 */		
		private var centerPoint:Point;
		/**
		 * 圆心坐标X和Y 
		 */		
		private var x:Number;
		private var y:Number;
		/**
		 * 内圆半径 
		 */		
		private var radiusIn:int;
		/**
		 * 外圆半径 
		 */		
		private var radiusOut:int;
		/**
		 * 弧线度数（角度0-360） 
		 */		
		private var arcDegree:int;
		/**
		 * 开始位置的弧度（顺时针） 
		 */		
		private var startAngle:int;
		/**
		 * 布尔值，指出是否绘制半径线把弧线连接至圆心
		 */		
		private var isArc:Boolean;
		/**
		 * 颜色数组 
		 */		
		private var colors:Array = null;
		/**
		 * 创建一个绘制实例，保存最基本的图形元素 
		 * @param x 圆心X
		 * @param y 圆心Y
		 * @param radIn 内圆半径
		 * @param radOut 外圆半径
		 * @param colours 颜色
		 * @param beginAngle 开始角度
		 * @param isArc 绘制和中心点的连线,true则不绘制，false则绘制
		 * 
		 */		
		public function CamberGraph(x:int,y:int,radIn:int,radOut:int,colours:Array,beginAngle:int,isArc:Boolean = true)
		{
			centerPoint = new Point(x,y);
			this.x = x;
			this.y = y;
			this.radiusIn = radIn;
			this.radiusOut = radOut;
			this.colors = colours;
			this.startAngle = beginAngle;
			this.isArc = isArc;
		}
		/**
		 * 根据角度，绘制该大小的环行
		 * @param target 图形实例
		 * @param arc 绘制的角度
		 * 
		 */		
		public function drawCirque(target:Graphics,arc:int):void{
			this.arcDegree = arc;
			target.clear();
		  	setStyle(target);
			draw(target);
			target.endFill();
		}
		/**
		 * 绘制环行
		 * @param target 图形实例
		 * 
		 */		
		private function draw(target:Graphics):void{
			if(arcDegree > 360){
				arcDegree = 360;
			}
			//夹角转化为弧度
			var arcRadian:Number = Math.PI/180 * arcDegree;
			//弧线的开始角度转化为弧度
			var startAngleRadian:Number = Math.PI/180 * startAngle;			
			//把弧度平均分为8段
			var nAngleDelta:Number = arcRadian/8;
			//分8段之后,求出圆心到二条切线交叉的距离
			var ctrlDircIn:Number = radiusIn/Math.cos(nAngleDelta/2);
			var ctrlDircOut:Number = radiusOut/Math.cos(nAngleDelta/2);
			//起始弧度
			var nAngle:Number = startAngleRadian;			
			var startInX:Number = radiusIn * Math.cos(startAngleRadian) + x;
			var startInY:Number = radiusIn * Math.sin(startAngleRadian) + y;
			var endInX:Number = radiusIn * Math.cos(startAngleRadian + arcRadian) + x;
			var endInY:Number = radiusIn * Math.sin(startAngleRadian + arcRadian) + y;
			var startOutX:Number = radiusOut * Math.cos(startAngleRadian) + x;
			var startOutY:Number = radiusOut * Math.sin(startAngleRadian) + y;
			var endOutX:Number = radiusOut * Math.cos(startAngleRadian + arcRadian) + x;
			var endOutY:Number = radiusOut * Math.sin(startAngleRadian + arcRadian) + y;
			target.moveTo(startInX, startInY);
			tmpDraw(target,nAngle,nAngleDelta,ctrlDircIn,radiusIn);
			if(isArc){
				target.lineTo(endOutX, endOutY);
			}
			else{
				target.moveTo(endOutX, endOutY);
			}			
			tmpDraw(target,startAngleRadian+arcRadian,-nAngleDelta,ctrlDircOut,radiusOut);
			if(isArc){
				target.lineTo(startInX, startInY);
			}			
		}
		/**
		 * 设置图形样式 
		 * @param target 图形实例
		 * 
		 */		
		private function setStyle(target:Graphics):void{
			var alphs:Array = [1,1];
			var ratios:Array = [0, 255];
			var matrix:Matrix = getMatrix(radiusIn*2,radiusIn*2,0,-radiusIn,-radiusIn);
			var fillType:String = GradientType.RADIAL;
			target.lineStyle(1);
			target.beginGradientFill(fillType,colors,alphs,ratios,matrix);
		}
		/**
		 * 塞贝尔曲线
		 * @param target 图形实例
		 * @param nAngle 角度
		 * @param nAngleDelta 
		 * @param ctrlDirc 
		 * @param radius 半径
		 * 
		 */		
		private function tmpDraw(target:Graphics, nAngle:Number, nAngleDelta:Number, ctrlDirc:Number, radius:Number):void{
			for(var i:uint = 0; i < 8; i++){
				nAngle += nAngleDelta;
				//求出控制点坐标
				var controlX:Number = Math.cos((nAngle - nAngleDelta/2)) * ctrlDirc + x;
			    var controlY:Number = Math.sin((nAngle - nAngleDelta/2)) * ctrlDirc + y;
				//求出画曲线的另一点坐标
				var anchorX:Number = Math.cos(nAngle) * radius + x;
				var anchorY:Number = Math.sin(nAngle) * radius + y;
				//画曲线线段
				target.curveTo(controlX,controlY,anchorX,anchorY);
			}
		}
		/**
		 * 获取圆形Matrix
		 * @param w
		 * @param h
		 * @param rotation
		 * @param tx
		 * @param ty
		 * @return 
		 * 
		 */		
		private function getMatrix(w:Number, h:Number, rotation:Number = 0, tx:Number = 0, ty:Number = 0):Matrix{
			var matrix:Matrix = new Matrix();
			rotation = rotation * Math.PI/180;
			matrix.createGradientBox(w, h, rotation, tx, ty);
			return matrix;
		}	
	}
}